﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;




public class NearestEnemySensor : BattleComponent<NearestEnemySensor>
{
    public Warrior MyAgent;
    public static int a;
    public Warrior NearestEnemy;
    public Ticker sensorTicker;


    private void OnEnable()
    {
        sensorTicker = new Ticker(30, true);
        Assign(this);
    }

    private void OnDisable()
    {
        Unassign(this);
    }

    public void UpdateNearestEnemy()
    {
        if (MyAgent == null) return;
        switch (MyAgent.unitOwner)
        {
            case PlayerType.AIEnemy:
                NearestEnemy = WarriorManager.Instance.GetNearestUnit(transform.position, PlayerType.Player);
                break;
            case PlayerType.Player:
                NearestEnemy = WarriorManager.Instance.GetNearestUnit(transform.position, PlayerType.AIEnemy);
                break;
        }
    }


    public static void Sense()
    {
        foreach(var sensor in ComponentDataList)
        {
            if (sensor.sensorTicker.Tick()) sensor.UpdateNearestEnemy();
        }
    }
}
